﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TjZheng.Quartz;
using TjZheng.Framework;
using TjZheng.Redis;
using CJAtech.BBS.Business;
using CJAtech.BBS.DataEntity;
using System.Threading;
using ServiceStack.Redis;

namespace TjZheng.SyncBBSRedis
{
    public class SyncArticleDiggCount : TjZhengJob
    {
        public override void JobExecute(global::Quartz.IJobExecutionContext context)
        {
            string str = "";
            int articleId = 0;
            int DiggCount = 0;
            IRedisClient objRedisClient;
            DbSetProcess objDbSetProcess = new DbSetProcess();
            string QueueKey = string.Format("{0}_Article_DiggCount_Queue", objDbSetProcess.BBSName);
            BizArticleInfo objBizArticleInfo = new BizArticleInfo(objDbSetProcess.ConnectionStringKey, "");//todo：连接串
            while (true)
            {
                objRedisClient = RedisHelper.GetClient("tjzheng", 1);
                ConsoleHelper.WriteLineYellow("【" + objDbSetProcess.BBSName + "更新文章点赞数】开始等待消息加入……");
                str = objRedisClient.BlockingDequeueItemFromList(QueueKey, null);
                ConsoleHelper.WriteLineYellow("【" + objDbSetProcess.BBSName + "更新文章点赞数】开始更新ID为【" + str + "】文章的评论数……");
                //str = objRedisClient.DequeueItemFromList(QueueKey);
                if (str != null)
                {
                    if (int.TryParse(str, out articleId) && articleId > 0)
                    {
                        str = objRedisClient.GetValueFromHash(string.Format("{0}_Article_RelatedCount_{1}", objDbSetProcess.BBSName, articleId), "DiggCount");
                        if (int.TryParse(str, out DiggCount) && DiggCount > 0)
                        {
                            objBizArticleInfo.Update("DiggCount=" + DiggCount, articleId);
                            ConsoleHelper.WriteLineYellow("【" + objDbSetProcess.BBSName + "更新文章点赞数】更新ID为【" + articleId + "】文章的点赞数【" + str + "】成功");
                        }
                        else
                        {
                            ConsoleHelper.WriteLineYellow("【" + objDbSetProcess.BBSName + "更新文章点赞数】转换文章ID【" + articleId + "】的评论数【" + str + "】失败");
                        }
                    }
                    else
                    {
                        ConsoleHelper.WriteLineYellow("【" + objDbSetProcess.BBSName + "更新文章点赞数】转换文章ID【" + str + "】格式失败");
                    }
                }
                objRedisClient.Dispose();
            }
        }
    }
}
